﻿using System;
using System.Collections.Generic;
using Aga.GestionDeArchivos.Web.Models;
using Aga.GestionDeArchivos.Services;
using System.Web;

namespace Aga.GestionDeArchivos.Web.Helpers
{
    public class MenuHelper
    {
        /// <summary>
        /// Builds the menu.
        /// </summary>
        /// <param name="userId">The user id.</param>
        /// <returns></returns>
        public List<MenuOption> BuildMainMenu(int userId)
        {
            List<MenuOption> menu = new List<MenuOption>();

            UserService service = new UserService();

            menu.Add(new MenuOption
            {
                Order = 1,
                Name = "Inicio",
                Key = MenuKey.Home,
                Url = VirtualPathUtility.ToAbsolute("~/"),
                SubMenuTitle = "Estamos en Contacto",
                Childs = new List<MenuOption> {
                        new MenuOption
                        {
                            Order = 1,
                            Name = "Ultimas Novedades",
                            Url = "#"
                        },
                        new MenuOption
                        {
                            Order = 2,
                            Name = "Blog",
                            Url = "#"
                        },
                        new MenuOption
                        {
                            Order = 3,
                            Name = "Consulta OnLine",
                            Url = "#"
                        },
                        new MenuOption
                        {
                            Order = 4,
                            Name = "Facebook",
                            Url = "#"
                        },
                        new MenuOption
                        {
                            Order = 5,
                            Name = "Twitter",
                            Url = "#"
                        },
                    }
            });

            // Administration
            int[] actions = new int[] { 
                (int)Actions.ViewUsers, 
                (int)Actions.ViewRoles, 
                (int)Actions.ViewClientes,
                (int)Actions.ViewDepositos,
                (int)Actions.ViewEstanterias,
                (int)Actions.GenerarEtiquetasCliente,
            };

            if (service.UserCan(userId, actions))
            {
                var menuOption = new MenuOption
                {
                    Order = 1,
                    Name = "Administración",
                    Key = MenuKey.Admin,
                    Url = "javascript:showSubMenu('" + MenuKey.Admin.ToString() + "')",
                    SubMenuTitle = "Administración"
                };

                if (service.UserCan(userId, (int)Actions.ViewUsers))
                {
                    menuOption.Childs.Add(new MenuOption
                    {
                        Order = 1,
                        Name = "Usuarios",
                        Url = VirtualPathUtility.ToAbsolute("~/Users")
                    });
                }

                if (service.UserCan(userId, (int)Actions.ViewRoles))
                {
                    menuOption.Childs.Add(new MenuOption
                    {
                        Order = 2,
                        Name = "Roles",
                        Url = VirtualPathUtility.ToAbsolute("~/Roles")
                    });
                }

                if (service.UserCan(userId, (int)Actions.ViewClientes))
                {
                    menuOption.Childs.Add(new MenuOption
                    {
                        Order = 3,
                        Name = "Clientes",
                        Url = VirtualPathUtility.ToAbsolute("~/Clientes")
                    });
                }

                if (service.UserCan(userId, (int)Actions.ViewDepositos))
                {
                    menuOption.Childs.Add(new MenuOption
                    {
                        Order = 4,
                        Name = "Depósitos",
                        Url = VirtualPathUtility.ToAbsolute("~/Depositos")
                    });
                }

                if (service.UserCan(userId, (int)Actions.ViewEstanterias))
                {
                    menuOption.Childs.Add(new MenuOption
                    {
                        Order = 5,
                        Name = "Estanterías",
                        Url = VirtualPathUtility.ToAbsolute("~/Estanterias")
                    });
                }

                if (service.UserCan(userId, (int)Actions.ImprimirEtiquetasUbicaciones))
                {
                    menuOption.Childs.Add(new MenuOption
                    {
                        Order = 6,
                        Name = "Etiquetas de Ubicaciones",
                        Url = VirtualPathUtility.ToAbsolute("~/Estanterias/ImprimirEtiquetas")
                    });
                }

                menu.Add(menuOption);
            }

            // Guarda
            actions = new int[] { 
                (int)Actions.GenerarEtiquetasCliente,
                (int)Actions.IngresoDeCaja,
                (int)Actions.EgresoDeCaja,
                (int)Actions.TrasladoDeCaja,
                (int)Actions.BuscarCaja,
                (int)Actions.SalidaCajaCerrada,
                (int)Actions.SalidaDocumentacion,
                (int)Actions.BuscarCaja,
                (int)Actions.RetornoCajaCerrada,
                (int)Actions.RetornoDocumentacion,
                (int)Actions.VerRemitos,
                (int)Actions.ViewItems
            };

            if (service.UserCan(userId, actions))
            {
                var menuOption = new MenuOption
                {
                    Order = 1,
                    Name = "Guarda",
                    Key = MenuKey.Guarda,
                    Url = "javascript:showSubMenu('" + MenuKey.Guarda.ToString() + "')",
                    SubMenuTitle = "Guarda"
                };

                if (service.UserCan(userId, (int)Actions.GenerarEtiquetasCliente))
                {
                    menuOption.Childs.Add(new MenuOption
                        {
                            Order = 1,
                            Name = "Cajas de Clientes",
                            Url = VirtualPathUtility.ToAbsolute("~/EtiquetasCliente")
                        });
                }

                if (service.UserCan(userId, (int)Actions.IngresoDeCaja))
                {
                    menuOption.Childs.Add(new MenuOption
                        {
                            Order = 2,
                            Name = "Ingreso de Caja",
                            Url = VirtualPathUtility.ToAbsolute("~/Guarda/Ingreso")
                        });
                }

                if (service.UserCan(userId, (int)Actions.EgresoDeCaja))
                {
                    menuOption.Childs.Add(new MenuOption
                        {
                            Order = 3,
                            Name = "Egreso de Caja",
                            Url = VirtualPathUtility.ToAbsolute("~/Guarda/Egreso")
                        });
                }

                if (service.UserCan(userId, (int)Actions.TrasladoDeCaja))
                {
                    menuOption.Childs.Add(new MenuOption
                    {
                        Order = 4,
                        Name = "Traslado de Caja",
                        Url = VirtualPathUtility.ToAbsolute("~/Guarda/Traslado")
                    });
                }

                if (service.UserCan(userId, (int)Actions.SalidaCajaCerrada))
                {
                    menuOption.Childs.Add(new MenuOption
                    {
                        Order = 5,
                        Name = "Salida en Consulta: Contenedor Cerrado",
                        Url = VirtualPathUtility.ToAbsolute("~/Guarda/SalidaCaja")
                    });
                }

                if (service.UserCan(userId, (int)Actions.SalidaDocumentacion))
                {
                    menuOption.Childs.Add(new MenuOption
                    {
                        Order = 6,
                        Name = "Salida en Consulta: Contenedor Abierto",
                        Url = VirtualPathUtility.ToAbsolute("~/Guarda/SalidaDocumentacion")
                    });
                }

                if (service.UserCan(userId, (int)Actions.RetornoCajaCerrada))
                {
                    menuOption.Childs.Add(new MenuOption
                    {
                        Order = 7,
                        Name = "Retorno Contenedor Cerrado",
                        Url = VirtualPathUtility.ToAbsolute("~/Guarda/RetornoCaja")
                    });
                }

                if (service.UserCan(userId, (int)Actions.RetornoDocumentacion))
                {
                    menuOption.Childs.Add(new MenuOption
                    {
                        Order = 8,
                        Name = "Retorno Documentación",
                        Url = VirtualPathUtility.ToAbsolute("~/Guarda/RetornoDocumentacion")
                    });
                }

                if (service.UserCan(userId, (int)Actions.VerRemitos))
                {
                    menuOption.Childs.Add(new MenuOption
                    {
                        Order = 9,
                        Name = "Remitos",
                        Url = VirtualPathUtility.ToAbsolute("~/Guarda/Remitos")
                    });
                }

                if (service.UserCan(userId, (int)Actions.BuscarCaja))
                {
                    menuOption.Childs.Add(new MenuOption
                    {
                        Order = 10,
                        Name = "Buscar Caja",
                        Url = VirtualPathUtility.ToAbsolute("~/Guarda/BuscarCaja")
                    });
                }

                if (service.UserCan(userId, (int)Actions.ViewItems))
                {
                    menuOption.Childs.Add(new MenuOption
                    {
                        Order = 11,
                        Name = "Items",
                        Url = VirtualPathUtility.ToAbsolute("~/Items")
                    });
                }

                menu.Add(menuOption);
            }


            return menu;
        }
    }

    public enum MenuKey
    {
        Home,
        Admin,
        Clientes,
        Contacto,
        Nosotros,
        Servicios,
        Guarda
    }
}